// Copyright (c) 2024 刻BITTER
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.


#pragma once


#if defined(PY32F002Ax5) || defined(PY32F002Bx5)
/*  # PY32F0xx    */

    #define _IOXX_GPIO_REGISTER_F0_STYLE (1)  // 设置引脚模式的各个寄存器互不影响，
                                              // 比如，设置好上下拉和输出类型后，切换引脚输入 / 输出模式后上下拉和输出类型都保持不变、
                                              // STM32F1 系列单片机就不行，存在寄存器复用

    #include <py32f0xx.h>

    #ifndef USE_FULL_LL_DRIVER
        #error "LL library is mandatory"
    #endif

    #if defined(PY32F002Bx5)
    /*  ## PY32F002B   */

        #define _IOXX_DEVICE_AVAILABLE (1)

        #include <py32f002b_ll_bus.h>
        #include <py32f002b_ll_gpio.h>

        #define _IOXX_GPIO_BUS IOP_GRP1

        #define _IOXX_GPIO_ALL_BUS_MASK (LL_IOP_GRP1_PERIPH_ALL)

        #define _IOXX_GPIO_ALTERNATE_SELECTOR_AVAILABLE (1)


    #endif

#elif defined(STM32F1) || defined(STM32F1XX) || defined(STM32F103x6) || defined(STM32F103x8) || defined(STM32F103xB) || defined(STM32F103xC) || defined(STM32F103xE) || defined(STM32F103xG)
// 对于Arduino 等没有硬件开漏输出模式的平台，可以采用切换引脚输入、输出模式的方法模拟开漏输出

    #define _IOXX_GPIO_REGISTER_F1_STYLE (1)
    #define _IOXX_DEVICE_AVAILABLE       (1)

    #ifndef STM32F1
        #define STM32F1 (1)
    #endif

    #include <stm32f1xx.h>

    #ifndef USE_FULL_LL_DRIVER
        #error "LL Lib Is Mandatory."
    #endif

    #include <stm32f1xx_ll_bus.h>
    #include <stm32f1xx_ll_gpio.h>

    #if defined(USE_HAL_DRIVER)
        #include <stm32f1xx_hal_gpio.h>
        #include <stm32f1xx_hal_i2c.h>
    #endif

    #define _IOXX_GPIO_BUS APB2_GRP1

    // 有时候不存在GPIOF 和GPIOG
    #ifdef LL_APB2_GRP1_PERIPH_GPIOF
        #define _CORTEX__M__GPIO__F LL_APB2_GRP1_PERIPH_GPIOF
    #else
        #define _CORTEX__M__GPIO__F (0)
    #endif

    #ifdef LL_APB2_GRP1_PERIPH_GPIOG
        #define _CORTEX__M__GPIO__G LL_APB2_GRP1_PERIPH_GPIOG
    #else
        #define _CORTEX__M__GPIO__G (0)
    #endif

    #define _IOXX_GPIO_ALL_BUS_MASK ( \
        LL_APB2_GRP1_PERIPH_AFIO      \
        | LL_APB2_GRP1_PERIPH_GPIOA   \
        | LL_APB2_GRP1_PERIPH_GPIOB   \
        | LL_APB2_GRP1_PERIPH_GPIOC   \
        | LL_APB2_GRP1_PERIPH_GPIOD   \
        | LL_APB2_GRP1_PERIPH_GPIOE   \
        | _CORTEX__M__GPIO__F         \
        | _CORTEX__M__GPIO__G)


#elif defined(HK32F030M) || defined(HK32F030MF4P6)

    #define _IOXX_GPIO_REGISTER_F0_STYLE (1)
    #define _IOXX_DEVICE_AVAILABLE       (1)

    #include <hk32f030m.h>
    #include <hk32f030m_adc.h>
    #include <hk32f030m_gpio.h>
    #include <hk32f030m_pwr.h>
    #include <hk32f030m_rcc.h>

    #define _IOXX_GPIO_BUS   AHB
    #define _IOXX_GPIO_BUS_2 APB1

    #define _IOXX_GPIO_ALL_BUS_MASK (RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD)

    #define _IOXX_GPIO_ALL_BUS_MASK_2 (RCC_APB1Periph_IOMUX)

    #define _IOXX_GPIO_ALTERNATE_SELECTOR_NOT_AVAILABLE (1)
    #define _IOXX_GPIO_SCHMIT_AVAILABLE                 (1)

#else

    #error "Device config not found."

#endif
